package com.哈希表;

import java.util.*;

/**
 * 给定两个数组，编写一个函数来计算它们的交集。
 *
 * 示例 1:
 *
 * 输入: nums1 = [1,2,2,1], nums2 = [2,2]
 * 输出: [2]
 * 示例 2:
 *
 * 输入: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
 * 输出: [9,4]
 */
public class 两个数组的交集 {
   static class Solution {
        public int[] intersection(int[] nums1, int[] nums2) {
            //手写map
//            MyHashMap myHashMap  = new MyHashMap();
            //jdk
            Map<Integer,Integer> myHashMap = new TreeMap<>();
            for(int i:nums1){

                    myHashMap.put(i,1);

            }
            Map<Integer,Integer> map2 = new TreeMap<>();

            for(int i:nums2){


                map2.put(i,1);

            }
            for(int num:myHashMap.keySet()){
                for(int num2:map2.keySet()){
                    if(num==num2){
                        myHashMap.put(num,2);
                        break;

                    }
                }
            }
            List<Integer> num3 = new ArrayList<>();
            for(int  i :myHashMap.keySet()){

                if(myHashMap.get(i) > 1){
                    num3.add(i);

                }
            }
            int[] num = new int[num3.size()];
            for(int i = 0;i<num3.size();i++){
                num[i] = num3.get(i);
            }
            return num;
        }

        public static void main(String[] args) {

            int[] num1  = {748,699,47,123,14,478,593,401,396,75,965,795,544,691,981,711,470,172};
            int[]  num2 = {748,699,47,123,14,478,593,401,396,75,965,795,544,691,981,711,470,172};
            Solution solution = new Solution();
           int num3[] = solution.intersection(num1,num2);

        }
    }
}
